package com.zenika.zenbinding.client.validator;

import com.zenika.zenbinding.client.exception.DataMessage;
import com.zenika.zenbinding.client.exception.ValidatorException;

public class EmailValidator implements Validator {

	public EmailValidator() {
	}

	public void validate(Object object) throws ValidatorException {
		if (object != null && !"".equals(object.toString())) {
			boolean isMatching = object
					.toString()
					.matches(
							"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
			if (!isMatching) {
				DataMessage message = new DataMessage("E-Mail Address is Not Valid");
				throw new ValidatorException(message);
			}
		}
	}
}
